package com.sinosoft.dsp.system.utils;

import java.security.MessageDigest;

import javax.measure.unit.Unit;

/**
 * 持久化的工具类
 * 
 * @author 潘巍（Peter Pan）
 * @since 2011-4-23 下午04:46:46
 */
public class PersistenceUtils {

	private static String digestAlgorithm = "SHA-1";

	private static String charset = "UTF-8";

	/**
	 * 通过名称获得单位
	 * 
	 * @param unit
	 * @return
	 */
	public static Unit<?> getUnit(String unit) {
		return null;
	}

	public static String getUnitName(Unit<?> unit) {
		return "";
	}

	public static String hash(String... texts) {
		try {
			String text = joint(texts);
			MessageDigest digest = MessageDigest.getInstance(digestAlgorithm);
			digest.update(text.getBytes(charset));
			byte[] rawHash = digest.digest();
			return new String(HashUtils.encode(rawHash));
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static String joint(String... strs) {
		if (strs != null) {
			StringBuffer rtn = new StringBuffer();
			for (String str : strs) {
				rtn.append(str);
				rtn.append(",");
			}
			return rtn.substring(0, rtn.length() - 1);
		}
		return "";
	}
}
